#include "addquestion.h"
#include "ui_addquestion.h"

addquestion::addquestion(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addquestion)
{
    ui->setupUi(this);
    this->setWindowTitle("增加试题");
    db = Database::getInstance().getDatabase();

}

addquestion::~addquestion()
{
    delete ui;
}

void addquestion::on_pushButton_add_clicked()
{
    int id = ui->lineEdit_id->text().toInt();
    QString text = ui->textEdit_text->toPlainText();
    QString optionA = ui->lineEdit_A->text().trimmed();
    QString optionB = ui->lineEdit_B->text().trimmed();
    QString optionC = ui->lineEdit_C->text().trimmed();
    QString optionD = ui->lineEdit_D->text().trimmed();

    if (text.isEmpty() || optionA.isEmpty() || optionB.isEmpty() || optionC.isEmpty() || optionD.isEmpty()) {
        QMessageBox::warning(this, "Input Error", "Please fill in all fields.");
        return;
    }

    QString options = optionA + "|" + optionB + "|" + optionC + "|" + optionD;

    question newQuestion(id,text, options);
    question Myquestion(0,"","");

    if (Myquestion.addQuestion(newQuestion)) {
        QMessageBox::information(this, "Success", "Question added successfully!");

    } else {
        QMessageBox::critical(this, "Error", "Failed to add question.");
    }

}

void addquestion::on_pushButton_exit_clicked()
{
    emit add_exit();
}

